var gulp = require('gulp'),
    
    // gulp dependencies
    less = require('gulp-less'),
    minifyCSS = require('gulp-minify-css'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    
    rename = require('gulp-rename'),
    clean = require('gulp-clean'),
    cache = require('gulp-cache'),
    autoprefixer = require('gulp-autoprefixer'),
    imagemin = require('gulp-imagemin'),
    jshint = require('gulp-jshint'),
    livereload = require('gulp-livereload'),
    notify = require('gulp-notify'),
    sourcemaps = require('gulp-sourcemaps')
;

// 配置文件路径
var paths = {
    dist: {
        minified: 'dist/compress',
        packaged: 'dist/',
    },
};

// js语法检查
gulp.task('checkjs', function(){
    return gulp.src(['src/js/*.js', 'src/js/**/*.js'])
            .pipe(jshint())
            .pipe(jshint.reporter('default'));
});

// 压缩js
gulp.task('minifyjs', function(){
    return gulp.src(['src/js/*.js', 'src/js/**/*.js'])
            .pipe(sourcemaps.init())
            .pipe(uglify())
            .pipe(rename({suffix: '.min'}))
            .pipe(sourcemaps.write('.'))
            .pipe(gulp.dest(paths.dist.minified + '/js'));
});

// 合并js
gulp.task('packagejs', function(){
    // jquery
    gulp.src(paths.dist.minified + '/js/jquery2.1.4.min.js')
        .pipe(concat('jquery.js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
    gulp.src([paths.dist.minified + '/js/jquery2.1.4.min.js.map'])
        .pipe(concat('jquery.map'))
        .pipe(rename({suffix: '.min.js'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
    // bootstrap
    gulp.src(paths.dist.minified + '/js/bootstrap3.3.5.min.js')
        .pipe(concat('bootstrap.js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
    gulp.src([paths.dist.minified + '/js/bootstrap3.3.5.min.js.map'])
        .pipe(concat('bootstrap.map'))
        .pipe(rename({suffix: '.min.js'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
    // semantic
    gulp.src(paths.dist.minified + '/js/semantic2.1.6.min.js')
        .pipe(concat('semantic.js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
    gulp.src([paths.dist.minified + '/js/semantic2.1.6.min.js.map'])
        .pipe(concat('semantic.map'))
        .pipe(rename({suffix: '.min.js'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
    // plugins
    gulp.src(paths.dist.minified + '/js/plugins/*.js')
        .pipe(concat('plugins.js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
    gulp.src([paths.dist.minified + '/js/plugins/*.js.map'])
        .pipe(concat('plugins.map'))
        .pipe(rename({suffix: '.min.js'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
    // custom
    gulp.src(paths.dist.minified + '/js/custom1.0.min.js')
        .pipe(concat('custom.js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
    gulp.src([paths.dist.minified + '/js/custom1.0.min.js.map'])
        .pipe(concat('custom.map'))
        .pipe(rename({suffix: '.min.js'}))
        .pipe(gulp.dest(paths.dist.packaged + '/js'));
});

// 压缩css
gulp.task('minifycss', function(){
    // 通过pipe() 把要处理的文件导向插件,通过查找对应插件的api执行对应的命令
    gulp.src(['src/css/*.css', 'src/css/**/*.css'])
        .pipe(sourcemaps.init())
        .pipe(minifyCSS())
        .pipe(rename({suffix: '.min'}))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(paths.dist.minified + '/css'));
});

// 合并css
gulp.task('packagecss', function(){
    // normalize
    gulp.src(paths.dist.minified + '/css/normalize3.0.2.min.css')
        .pipe(concat('normalize.css'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.dist.packaged + '/css'));
    gulp.src([paths.dist.minified + '/css/normalize3.0.2.min.css.map'])
        .pipe(concat('normalize.map'))
        .pipe(rename({suffix: '.min.css'}))
        .pipe(gulp.dest(paths.dist.packaged + '/css'));
    // bootstrap
    gulp.src(paths.dist.minified + '/css/bootstrap3.3.5.min.css')
        .pipe(concat('bootstrap.css'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.dist.packaged + '/css'));
    gulp.src([paths.dist.minified + '/css/bootstrap3.3.5.min.css.map'])
        .pipe(concat('bootstrap.map'))
        .pipe(rename({suffix: '.min.css'}))
        .pipe(gulp.dest(paths.dist.packaged + '/css'));
    // semantic
    gulp.src(paths.dist.minified + '/css/semantic2.1.6.min.css')
        .pipe(concat('semantic.css'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.dist.packaged + '/css'));
    gulp.src([paths.dist.minified + '/css/semantic2.1.6.min.css.map'])
        .pipe(concat('semantic.map'))
        .pipe(rename({suffix: '.min.css'}))
        .pipe(gulp.dest(paths.dist.packaged + '/css'));
    // custom   
    gulp.src(paths.dist.minified + '/css/custom1.0.min.css')
        .pipe(concat('custom.css'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(paths.dist.packaged + '/css'));
    gulp.src([paths.dist.minified + '/css/custom1.0.min.css.map'])
        .pipe(concat('custom.map'))
        .pipe(rename({suffix: '.min.css'}))
        .pipe(gulp.dest(paths.dist.packaged + '/css'));
});

// Copy all static images
gulp.task('packageimg', function() {
  return gulp.src(['src/images/*.*', 'src/images/**/*.*'])
    .pipe(imagemin({
        progressive: true,
        svgoPlugins: [{removeViewBox: false}]
    }))
    .pipe(gulp.dest(paths.dist.packaged + '/images'));
});

// Copy all font
gulp.task('fonts', function() {
  return gulp.src(['src/font/*.*'])
    // Pass in options to the task
    .pipe(gulp.dest(paths.dist.packaged + '/font'));
});

// 批量命令
gulp.task('comprass', ['minifycss', 'minifyjs'], function(){
    gulp.start('packagecss', 'packagejs','fonts');
});

// 清理所有文件
gulp.task('clean', function () {
    return gulp.src('dist', {read: false})
        .pipe(clean());
});

// 清理js文件
gulp.task('cleanjs', function () {
    return gulp.src('dist/js', {read: false})
        .pipe(clean());
});

// 清理css文件
gulp.task('cleancss', function () {
    return gulp.src('dist/css', {read: false})
        .pipe(clean());
});

// 清理image文件
gulp.task('cleanimg', function () {
    return gulp.src('dist/images', {read: false})
        .pipe(clean());
});

// 清理font文件
gulp.task('cleanfont', function () {
    return gulp.src('dist/font', {read: false})
        .pipe(clean());
});

// 设置默认任务
gulp.task('default', ['cleanjs','cleancss','cleanfont'], function(){
    gulp.start('comprass');
});

// 执行images任务
gulp.task('images', ['cleanimg'], function(){
    gulp.start('packageimg');
});

// 执行all任务
gulp.task('all', ['clean'], function(){
    gulp.start('comprass', 'packageimg');
});

// 监听事件
gulp.task('watch', function(){
    // 监听文件是否修改，以便执行相应的任务
    gulp.watch('src/css/*.css', ['minifycss']);
    gulp.watch('src/css/**/*.css', ['minifycss']);
    gulp.watch('src/js/*.js', ['minifyjs']);
    gulp.watch('src/js/**/*.js', ['minifyjs']);
});
